﻿using MediatR;
using ProcurementManagement.Applcation.Command.QualityCommand;
using ProcurementManagement.Domain.Dtos.Quality;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Dtos;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.QualityCommandHandler
{
    public class SelectProQAugitCommandHandlers : IRequestHandler<SelectProQAugitCommand, PageDTO<SelectProAuditDTO>>
    {
        private readonly IProductQualityRepository qualityRepository;
        private readonly IPurchaseContractsRepository _purchaseContractsRep;
        private readonly ISourcProductContractRepository _sourceProductContractRep;
        private readonly InterfaceVendorRepository _vendorRep;
        ISqlSugarClient db;

        public SelectProQAugitCommandHandlers(IProductQualityRepository qualityRepository, IPurchaseContractsRepository purchaseContractsRep, ISourcProductContractRepository sourceProductContractRep, InterfaceVendorRepository vendorRep, ISqlSugarClient db)
        {
            this.qualityRepository = qualityRepository;
            _purchaseContractsRep = purchaseContractsRep;
            _sourceProductContractRep = sourceProductContractRep;
            _vendorRep = vendorRep;
            this.db = db;


        }

        public async Task<PageDTO<SelectProAuditDTO>> Handle(SelectProQAugitCommand request, CancellationToken cancellationToken)
        {
            var list = db.Queryable<ProductQuality, PurchaseContracts, SourcProductContract, Vendor>((pq, pc, spc, v) => new
            JoinQueryInfos(
                JoinType.Inner, pq.PurchaseContractsIds == pc.PurchaseContractsIds,
                 JoinType.Inner, pc.PCNumber == spc.PCNumber,
                JoinType.Inner, pc.VendorId == v.VendorId))
                .Where((pq, pc, spc, v) => pq.IsDel == false)

                .Select((pq, pc, spc, v) => new SelectProAuditDTO
                {
                    QualityProId = pq.ProQualityId,
                    ProAppId = pq.ProAppId,
                    ProContract = pq.ProContract,
                    PCName = pc.PCName,
                    PurchaseContractsIds = pc.PurchaseContractsIds,
                    ApplyName = pq.ApplyName,
                    AuditResults = pq.AuditResults,
                    QcOutcome = pq.QcOutcome,
                    QcPerson = pq.QcPerson,
                    QcState = pq.QcState,
                    QcTime = pq.QcTime,
                    QualityName = pq.QualityName,
                    QualityNumber = pq.QualityNumber,
                    QualityType = pq.QualityType,
                    SPCArrivalDate = spc.SPCArrivalDate,
                    SPCPrice = spc.SPCPrice,
                    SPCNum = spc.SPCNum,
                    Qualified = pq.Qualified,
                    SPCTotalPrice = spc.SPCTotalPrice,
                    SPPName = spc.SPPName,
                    SPPNumber = spc.SPPNumber,
                    SPPSpec = spc.SPPSpec,
                    Unqualified = pq.Unqualified,
                    Vname = v.VName,
                    AugitName = pq.AugitName,



                }).ToList();

            if (!string.IsNullOrEmpty(request.Qcode))
            {
                list = list.Where(x => x.QualityNumber.Contains(request.Qcode)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Qname))
            {
                list = list.Where(x => x.QualityName.Contains(request.Qname)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Stime))
            {
                list = list.Where(x => x.QcTime >= DateTime.Parse(request.Stime)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Etime))
            {
                list = list.Where(x => x.QcTime < DateTime.Parse(request.Etime)).ToList();
            }
            if (request.Qstatus > 0)
            {
                list = list.Where(x => x.QcState == request.Qstatus).ToList();
            }
            if(request.AuditResults!= null)
            {
                list = list.Where(x => x.AuditResults == request.AuditResults).ToList();
            }

            if (request.QcOutcome !=null)
            {
                list = list.Where(x => x.QcOutcome == request.QcOutcome).ToList();
            }

            int totalcount = list.Count();
            int pagecount = (int)Math.Ceiling(totalcount / (double)request.pageSize);


            var pagedto= list.OrderByDescending(x => x.QualityProId).Skip((request.PageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

    
            var pages = new PageDTO<SelectProAuditDTO>();
            pages.Pagecount = pagecount;
            pages.Totalcount = totalcount;
            pages.list = pagedto;

            return pages;
        }
    }
}
